#include "Knight.h"

Knight::Knight(Map &map, int x, int y) : Unit(map) {
    Move(x, y);
    Image *image = new Image();
    image->LoadFromFile("gfx/knight.png");

    for (int i=DIR_S; i<=DIR_N; i++) {
        sprites[i].SetImage(*image);
        sprites[i].SetSubRect(IntRect(64*i, 0, 64*(i+1), 64));
        sprites[i].SetCenter(0, 15);
    }

    sprites[DIR_SE] = sprites[DIR_SW];
    sprites[DIR_SE].FlipX(true);

    sprites[DIR_E] = sprites[DIR_W];
    sprites[DIR_E].FlipX(true);

    sprites[DIR_NE] = sprites[DIR_NW];
    sprites[DIR_NE].FlipX(true);
}

Knight::~Knight() {
}

std::vector<Vector2i> Knight::GetActions(int actionType) {
    std::vector <Vector2i> actions;
    if (health <= 0) {
        return actions;
    }
    const int move_array[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    const int attack_array[8][2] = {{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1}};
    const int rotate_array[8][2] = {{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1}};
    // movement action
    if (actionType == ACT_MOVE) {
        for (int i=0; i<4; i++) {
            int newX = x+move_array[i][0];
            int newY = y+move_array[i][1];
            Tile *tile = map.GetTile(newX, newY);
            if (tile != NULL && tile->unit == NULL) {
                actions.push_back(Vector2i(newX, newY));
            }
        }
    }
    // attack action
    else if (actionType == ACT_ATTACK) {
        for (int i=0; i<1; i++) {
            int newX = x+attack_array[dir][0];
            int newY = y+attack_array[dir][1];
            Tile *tile = map.GetTile(newX, newY);
            if (tile != NULL) {
                actions.push_back(Vector2i(newX, newY));
            }
        }
    }
    else if (actionType == ACT_ROTATE) {
        for (int i=0; i<8; i++) {
            int newX = x+rotate_array[i][0];
            int newY = y+rotate_array[i][1];
            actions.push_back(Vector2i(newX, newY));
        }
    }
    return actions;
}

void Knight::Die() {
    Unit::Die();
}

void Knight::Update(float dt) {
    Unit::Update(dt); 
}

void Knight::Render(SpriteManager &spriteManager) {
    Unit::Render(spriteManager);
}
